<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="月计划单" prop="planId">
        <el-input v-model="queryParams.planId" placeholder="请输入月计划单" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="施工单位" prop="buildUnitId">
        <el-input v-model="queryParams.buildUnitId" placeholder="请输入施工单位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标段" prop="lotSection">
        <el-input v-model="queryParams.lotSection" placeholder="请输入标段" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人" prop="contactsId">
        <el-input v-model="queryParams.contactsId" placeholder="请输入联系人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="到货点" prop="deliveryPoint">
        <el-input v-model="queryParams.deliveryPoint" placeholder="请输入到货点" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-wallet" size="mini" @click="handlerProcess"
          v-hasPermi="['sale:delivery:remove']">审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-document" size="mini" @click="handlerDetail"
          v-hasPermi="['sale:batch:add']">详情</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['sale:batch:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['sale:batch:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['sale:batch:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="batchList" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="serialId" />
      <el-table-column label="月计划单" align="center" prop="planId" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <!-- <el-table-column label="施工单位" align="center" prop="buildUnitId" />
      <el-table-column label="标段" align="center" prop="lotSection" /> -->
      <!-- <el-table-column label="联系人" align="center" prop="contactsId" /> -->
      <!-- <el-table-column label="联系人电话" align="center" prop="contactsTel" /> -->
      <!-- <el-table-column label="批号" align="center" prop="batchName" /> -->
      <el-table-column label="到货点" align="center" prop="deliveryPoint" />
      <el-table-column label="运输地址" align="center" prop="transportAddr" />
      <!-- <el-table-column label="供应合同" align="center" prop="contractId" /> -->
      <el-table-column label="到货日期" align="center" prop="arrivalDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本月已供数" align="center" prop="supplied" />
      <!-- <el-table-column label="审核人" align="center" prop="reviewedBy" /> -->
      <el-table-column label="审核状态" align="center" prop="reviewedStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sms_audit_status" :value="scope.row.reviewedStatus" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="审核时间" align="center" prop="reviewedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="启用状态" align="center" prop="status" /> -->
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column label="操作" width="150" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handlerDetail(scope.row)"
            v-hasPermi="['sale:batch:edit']">详情</el-button>
          <el-button v-show="scope.row.reviewedStatus == 0" size="mini" type="text" icon="el-icon-wallet"
            @click="handlerProcess($event, scope.row)" v-hasPermi="['sale:delivery:process']">审核</el-button>
          <el-button v-show="scope.row.reviewedStatus == 1" size="mini" type="text" icon="el-icon-bangzhu"
            @click="handlerDispatch(scope.row)" v-hasPermi="['sale:delivery:process']">发运调度</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['sale:batch:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改计划分批对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="批号" prop="serialId" >
          <el-input v-model="form.serialId" placeholder="请输入批号" disabled/>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactsTel">
          <el-input v-model="form.contactsTel" placeholder="请输入联系人电话" />
        </el-form-item>
        
        <el-form-item label="到货点" prop="deliveryPoint">
          <el-input v-model="form.deliveryPoint" placeholder="请输入到货点" />
        </el-form-item>
        <el-form-item label="运输地址" prop="transportAddr">
          <el-input v-model="form.transportAddr" placeholder="请输入运输地址" />
        </el-form-item>
        <el-form-item label="到货日期" prop="arrivalDate">
          <el-date-picker clearable v-model="form.arrivalDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择到货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="reviewedBy">
          <el-input v-model="form.reviewedBy" placeholder="请输入审核人" />
        </el-form-item>
        <div class="materialTab">
            <el-table :data="form.batchPlanDetails" size="mini" border style="width: 100%" max-height="220">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column label="材料名称" prop="materialName" :show-overflow-tooltip="true" />
              <el-table-column label="规格型号" prop="specifications" :show-overflow-tooltip="true" />
              <el-table-column label="钢号" prop="steelNumber" :show-overflow-tooltip="true" />
              <el-table-column label="类型" prop="meterialTypeId" :show-overflow-tooltip="true" />
              <el-table-column label="批次数量" align="center" prop="splitQuantity" />
            </el-table>
          </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBatch, getBatchDetailsWithSerialId,getBatch, delBatch, processBatch, addBatch, updateBatch } from "@/api/sale/batch";

export default {
  name: "Batch",
  dicts: ['sms_audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 计划分批表格数据
      batchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      checkRow: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planId: null,
        projectName: null,
        buildUnitId: null,
        lotSection: null,
        contactsId: null,
        deliveryPoint: null,
      },
      // 表单参数
      form: {batchPlanDetails:[]},
      // 表单校验
      rules: {
        planId: [
          { required: true, message: "月计划单不能为空", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        buildUnitId: [
          { required: true, message: "施工单位不能为空", trigger: "blur" }
        ],
        lotSection: [
          { required: true, message: "标段不能为空", trigger: "blur" }
        ],
        contactsId: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactsTel: [
          { required: true, message: "联系人电话不能为空", trigger: "blur" }
        ],
        batchName: [
          { required: true, message: "批号不能为空", trigger: "blur" }
        ],
        deliveryPoint: [
          { required: true, message: "到货点不能为空", trigger: "blur" }
        ],
        transportAddr: [
          { required: true, message: "运输地址不能为空", trigger: "blur" }
        ],
        contractId: [
          { required: true, message: "供应合同不能为空", trigger: "blur" }
        ],
        arrivalDate: [
          { required: true, message: "到货日期不能为空", trigger: "blur" }
        ],
        supplied: [
          { required: true, message: "本月已供数不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "制单人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "制单日期不能为空", trigger: "blur" }
        ],
        editedBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ],
        editedTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
        isDeleted: [
          { required: true, message: "删除状态不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "启用状态不能为空", trigger: "change" }
        ],
        note: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRowClick(row) {
      // console.log(row,'rrrrw')
      // if (row['reviewedStatus'] != 0) {
      //   let planId = row['planId'];
      //   this.checkRow = row
      // }
    },
    handlerDispatch(row) {
      //弹框，然后选择线路和承运商
      console.log('row', row)
    },
    handlerProcess(click, row) {
      const r = row || this.checkRow
      console.log()
      this.$modal.confirm('是否通过"' + r['projectName'] + '"项目计划？').then(function () {
      }).then(() => {
        //发送请求，修改审核状态
        processBatch(r.serialId).then(response => {
          this.getList();
          this.$modal.msgSuccess("审核通过");
        })
      }).catch(() => { })
    },
    handlerDetail(row) {
      console.log(row,'rowww')
      this.form = row;
      this.open = true
      let res
      getBatchDetailsWithSerialId(row.serialId).then(response => {
        res = response.rows
        console.log(response,'response..')
      })
      setTimeout(() => {
      this.form['batchPlanDetails'] = res 
        
      }, 0);

    },  
    /** 查询计划分批列表 */
    getList() {
      this.loading = true;
      listBatch(this.queryParams).then(response => {
        this.batchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serialId: null,
        planId: null,
        projectName: null,
        buildUnitId: null,
        lotSection: null,
        contactsId: null,
        contactsTel: null,
        batchName: null,
        deliveryPoint: null,
        transportAddr: null,
        contractId: null,
        arrivalDate: null,
        supplied: null,
        createBy: null,
        createTime: null,
        editedBy: null,
        editedTime: null,
        isDeleted: null,
        reviewedBy: null,
        reviewedStatus: null,
        reviewedTime: null,
        status: null,
        note: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      if (selection.length == 1) {
        this.checkRow = selection[0];
      } else {
        this.checkRow = null
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加计划分批";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serialId = row.serialId || this.ids
      getBatch(serialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改计划分批";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialId != null) {
            updateBatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialIds = row.serialId || this.ids;
      this.$modal.confirm('是否确认删除计划分批编号为"' + serialIds + '"的数据项？').then(function () {
        return delBatch(serialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sale/batch/export', {
        ...this.queryParams
      }, `batch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
